
//
//  CoursePageTitleView.m
//  ShowYork
//
//  Created by hxh on 2020/8/28.
//  Copyright © 2020 hxh. All rights reserved.
//

#import "LessonPageTitleView.h"

@interface LessonPageTitleView ()
@property (nonatomic,strong)UILabel *titleLb;
@property (nonatomic,strong)UIButton *moreBtn;
@property (nonatomic,strong)UIView *moreView;
@property (nonatomic,copy)NSString *titleName;
@property (nonatomic,strong)UIView *line;
@property (nonatomic,assign)BOOL hideLine;
@end

@implementation LessonPageTitleView

- (instancetype)initWithTitleName:(NSString *)titleName hideLine:(BOOL)hideLine{
    if (self = [super init]) {
        self.titleName = titleName;
        self.hideLine = hideLine;
        [self setUI];
    }
    return self;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setUI];
    }
    return self;
}

- (void)setUI{
    
    self.backgroundColor = [UIColor whiteColor];
    
    self.titleLb = [[UILabel alloc]init];
    self.titleLb.font = [UIFont boldSystemFontOfSize:18];
    self.titleLb.textColor = RGBA(35, 24, 21, 1);
    self.titleLb.text = self.titleName;
    [self addSubview:self.titleLb];
    
    self.moreBtn = [[UIButton alloc]init];
    [self.moreBtn setTitle:@"查看更多 >" forState:UIControlStateNormal];
    [self.moreBtn setTitleColor:RGBA(153, 153, 153, 1) forState:UIControlStateNormal];
    self.moreBtn.titleLabel.font = font(12);
    [self.moreBtn addTarget:self action:@selector(moreMethod) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.moreBtn];
    
    if (!self.hideLine) {
        self.line = [[UIView alloc]init];
        self.line.backgroundColor = RGBA(248, 248, 248, 1);
        [self addSubview:self.line];
        [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.right.equalTo(self);
            make.height.mas_equalTo(8);
        }];
        [self.titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.line.mas_bottom).offset(30);
            make.left.equalTo(self).offset(16);
        }];
    }else{
        [self.titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self).offset(22);
            make.left.equalTo(self).offset(16);
        }];
    }
    [self.moreBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self).offset(-16);
        make.centerY.equalTo(self.titleLb);
        make.height.mas_equalTo(30);
        make.width.mas_equalTo(80);
    }];
    
    self.moreBtn.hidden = YES;
}
- (void)moreMethod{
    if (self.delegate && [self.delegate respondsToSelector:@selector(showMoreAtIndex:)]) {
        [self.delegate showMoreAtIndex:self.index];
    }
}

- (void)setTitle:(NSString *)title{
    self.titleLb.text = title;
    if ([title isEqualToString:@"热门课程"]) {
        self.moreBtn.hidden = YES;
    }else if([title isEqualToString:@"课程推荐"]){
        self.moreBtn.hidden = NO;
    }
}
- (void)setIndex:(NSInteger)index{
    self.moreBtn.hidden = index == 1;
}

@end
